#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class DepartmentProvider : CY.GFive.Core.Providers.DALProvider.IDepartmentProvider
	{
		#region Sql
		private static readonly string SqlSelectDepartment = "SELECT [Id], [DepartCode],[DepartName] FROM [Department] WHERE  [Id]=@Id";

		private static readonly string SqlInsertDepartment = "INSERT INTO [Department]([DepartCode],[DepartName]) values(@DepartCode,@DepartName);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateDepartment = "UPDATE [Department] SET [DepartCode] = @DepartCode,[DepartName] = @DepartName WHERE  [Id]=@Id";

		private static readonly string SqlDeleteDepartment = "DELETE [Department] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllDepartment = "SELECT [Id], [DepartCode],[DepartName] FROM [Department]";
		#endregion

		#region IDepartmentProvider Members
		public Core.Business.Department Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectDepartment);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.Department department = new Core.Business.Department();

				if (!reader.IsDBNull(0)) department.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) department.DepartCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) department.DepartName = reader.GetString(2);

				return department;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.Department department)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@DepartCode", SqlDbType.NVarChar, department.DepartCode);
			sql.AddParameter("@DepartName", SqlDbType.NVarChar, department.DepartName);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertDepartment);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) department.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.Department department)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@DepartCode", SqlDbType.NVarChar, department.DepartCode);
			sql.AddParameter("@DepartName", SqlDbType.NVarChar, department.DepartName);
			sql.AddParameter("@Id", SqlDbType.Int, department.Id);
			sql.ExecuteSql(SqlUpdateDepartment);
		}

		public void Delete(Core.Business.Department department)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, department.Id);
			sql.ExecuteSql(SqlDeleteDepartment);
		}

		public IList<Core.Business.Department> GetAllDepartment()
		{
			IList<Core.Business.Department> departmentlist = new List<Core.Business.Department>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllDepartment);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.Department department = new Core.Business.Department();

					if (!reader.IsDBNull(0)) department.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) department.DepartCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) department.DepartName = reader.GetString(2);

					department.MarkOld();
					departmentlist.Add(department);
				}
				reader.Close();
			}
			return departmentlist;
		}
		#endregion
	}
}
